home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / fido_top.zip / FIDO-TOP.C next >
C/C++ Source or Header  |  1991-05-13  |  18KB  |  604 lines

  1. /*
  2.    
  3.                                   FIDO-TOP
  4.                                 Version 1.01
  5.              Copyright (c) Bob Swift, 1991.  All Rights Reserved.
  6.             Compiled with the Turbo C++ 1.0 compiler from Borland.
  7.    
  8.    This program is used to read the Fido 12 CALLER.SYS files and generate a
  9.    file listing the top users (upload, download, calls) for the BBS.  The
  10.    CALLER.SYS file must be in the current directory.
  11.  
  12.    The program will display a VERY brief set of instructions if it is called
  13.    without any arguments or if it encounters an error.  The following is a 
  14.    list of the error codes returned by the program:
  15.    
  16.                   0 - No errors.  Normal termination.
  17.                   1 - Missing/Bad/Extra command line argument.
  18.                   2 - Unable to open the CALLER.SYS file.
  19.                   3 - Unable to open the output file.
  20.                   4 - Unable to write the output file.
  21.                   5 - Unable to close the CALLER.SYS file.
  22.                   6 - Unable to close the output file.
  23.                   7 - Output file not in current directory. (disabled)
  24.                   8 - Unable to create output backup file.
  25.                   9 - No user records to process.
  26.    
  27.    When an error is encountered, the program will exit immediately and will
  28.    attempt to properly close all files.
  29.    
  30.    Although I have chosen to retain all rights to this program, you are free
  31.    to use it under the following conditions:
  32.    
  33.             - You realize that there is NO Warrantee of any sort.
  34.               It was tested pretty thoroughly here before release
  35.               but who knows what bugs may be lurking within.
  36.             
  37.             - You will not modify the code and release a new version
  38.               of the program.  I welcome suggestions for improvement
  39.               (especially when accompanied by code) but I make no
  40.               guarantee of future releases.
  41.             
  42.             - If you find the program useful, I ask that you do 
  43.               something to brighten somebody else's day.  Just 
  44.               exactly what, I will leave up to you.
  45.  
  46.    
  47.    You may freely distribute this program provided that you distribute only 
  48.    the complete archive which includes the FIDO-TOP.EXE, FIDO-TOP.C and 
  49.    FIDO-TOP.DOC.  In addition, You MUST NOT charge for the program nor may
  50.    you charge a copy fee over $4.00 (including the price of the diskette).  
  51.    
  52.    
  53.                                                    Bob Swift (1:140/24)
  54.  
  55.  
  56.  
  57.  
  58.    Revision History
  59.    ----------------
  60.  
  61.    1.00     91/05/11    First release version.
  62.  
  63.    1.01     91/05/13    Modified first line of output for Bulletin title.
  64.  
  65. */
  66.  
  67. #define TITLE        "FIDO-TOP"                 /*  Program Name      */
  68. #define DATES        "1991"                    /*  Copyright Date    */
  69. #define VERSION      "1.01"                    /*  Version Number    */
  70.  
  71. #define NUM_TO_LIST     10                     /*  List Top n Users  */
  72.  
  73. #include <stdio.h>
  74. #include <string.h> 
  75. #include <time.h> 
  76. #include "fido.h"
  77.  
  78. void helpscrn(int ernum);
  79. void add_period(char filename[]);
  80. void build_bak_name(char filename[]);
  81. int is_in_dir(char *filename);
  82. int del_backup(char *filename);
  83. int make_backup(char *srcfile, char *destfile);
  84. char *pad_string(char *str_to_pad, int str_len);
  85. char *add_commas(unsigned ul);
  86.  
  87. /*******************************************************************/
  88.  
  89. void main(int argc, char *argv[])
  90. {
  91.  
  92. struct tm *tm_now;
  93. time_t secs_now;
  94.  
  95. FILE *infile1,*outfile;
  96.  
  97. char flnames[2][40];
  98.  
  99. char outline[128];
  100.  
  101. static struct _clr fido_data;
  102.  
  103. char version[] = {VERSION};  /* Current Version Number */
  104. char title[] = {TITLE};
  105. char dates[] = {DATES};
  106.  
  107. int i,
  108.     j,
  109.     k,
  110.     l,
  111.     numtolist,
  112.     u_flag,
  113.     d_flag,
  114.     c_flag;
  115.  
  116. char u_name[NUM_TO_LIST][20],
  117.      d_name[NUM_TO_LIST][20],
  118.      c_name[NUM_TO_LIST][20];
  119.  
  120. char u_call[NUM_TO_LIST][20],
  121.      d_call[NUM_TO_LIST][20],
  122.      c_call[NUM_TO_LIST][20];
  123.  
  124. unsigned int  c_calls[NUM_TO_LIST];         /* number of calls */
  125.  
  126. unsigned int  dl_bytes[NUM_TO_LIST],        /* total bytes downloaded */
  127.               ul_bytes[NUM_TO_LIST];        /* total bytes uploaded */
  128.  
  129. int u_index[NUM_TO_LIST],
  130.     d_index[NUM_TO_LIST],
  131.     c_index[NUM_TO_LIST];
  132.  
  133. numtolist = NUM_TO_LIST;
  134.  
  135. printf("\n\n%s Version %s\n",title,version);
  136. printf("Copyright (c) Bob Swift, %s.  All Rights Reserved.",dates);
  137.  
  138. if (argc < 2 || argc > 3) helpscrn(1);
  139. strcpy(flnames[0],strupr(argv[1]));             /* Output File  */
  140. strcpy(flnames[1],strupr(argv[1]));             /* Backup File  */
  141.  
  142. /*   if (is_in_dir(flnames[2]) == 1)   */
  143. /*       helpscrn(7);                  */
  144.  
  145. if ((infile1=fopen("CALLER.SYS","rb")) == NULL)
  146.     helpscrn(2);
  147.  
  148. add_period(flnames[1]);
  149.  
  150. build_bak_name(flnames[1]);
  151.  
  152. /*  Exit if output file has an extension of BAK  */
  153. if (strcmp(flnames[0],flnames[1])==0)
  154.    helpscrn(8);
  155.  
  156. if (del_backup(flnames[1]) == 1)
  157.     helpscrn(8);
  158.  
  159. if (make_backup(flnames[0],flnames[1]) == 1)
  160.     helpscrn(8);
  161.  
  162. if ((outfile=fopen(flnames[0],"w")) == NULL)
  163.     helpscrn(3);
  164.  
  165. printf("\n\nInitializing.");
  166.  
  167. u_flag = 0;
  168. d_flag = 0;
  169. c_flag = 0;
  170.  
  171. if (argc > 2)
  172.     {
  173.     if (strchr(argv[2],'U')!=NULL || strchr(argv[2],'u')!=NULL)
  174.         u_flag = 1;
  175.     if (strchr(argv[2],'D')!=NULL || strchr(argv[2],'d')!=NULL)
  176.         d_flag = 1;
  177.     if (strchr(argv[2],'C')!=NULL || strchr(argv[2],'c')!=NULL)
  178.         c_flag = 1;
  179.     }
  180. else
  181.     {
  182.     u_flag = 1;
  183.     d_flag = 1;
  184.     c_flag = 1;
  185.     }
  186.  
  187. if (u_flag == 0 && c_flag == 0 && d_flag == 0)
  188.     helpscrn(1);
  189.  
  190. i = 0;
  191.  
  192. while (i < numtolist)
  193.     {
  194.     u_index[i] = i;
  195.     d_index[i] = i;
  196.     c_index[i] = i;
  197.     u_name[i][0] = '\0';
  198.     d_name[i][0] = '\0';
  199.     c_name[i][0] = '\0';
  200.     u_call[i][0] = '\0';
  201.     d_call[i][0] = '\0';
  202.     c_call[i][0] = '\0';
  203.     c_calls[i] = 0;
  204.     ul_bytes[i] = 0;
  205.     dl_bytes[i] = 0;
  206.     i++;
  207.     }
  208.  
  209. printf("\n");
  210.  
  211. i = 0;
  212.  
  213. while (fread(&fido_data,sizeof(struct _clr),1,infile1) == 1)
  214. {
  215.   i++;
  216.   printf("\rProcessing User %d",i);
  217.  
  218.   if (c_flag == 1)
  219.     {
  220.     j = 0;
  221.     while (j < numtolist)
  222.         {
  223.         if (fido_data.times > c_calls[c_index[j]])
  224.             {
  225.             strcpy(c_name[c_index[numtolist-1]],fido_data.name);
  226.             strcpy(c_call[c_index[numtolist-1]],fido_data.date);
  227.             c_calls[c_index[numtolist-1]] = fido_data.times;
  228.             l = c_index[numtolist-1];
  229.             k = numtolist - 1;
  230.             while (k > j)
  231.                 {
  232.                 c_index[k] = c_index[k-1];
  233.                 k--;
  234.                 }
  235.             c_index[j] = l;
  236.             j = numtolist;
  237.             }
  238.         j++;
  239.         }
  240.     }
  241.  
  242.   if (d_flag == 1)
  243.     {
  244.     j = 0;
  245.     while (j < numtolist)
  246.         {
  247.         if (fido_data.dnld > dl_bytes[d_index[j]])
  248.             {
  249.             strcpy(d_name[d_index[numtolist-1]],fido_data.name);
  250.             strcpy(d_call[d_index[numtolist-1]],fido_data.date);
  251.             dl_bytes[d_index[numtolist-1]] = fido_data.dnld;
  252.             l = d_index[numtolist-1];
  253.             k = numtolist - 1;
  254.             while (k > j)
  255.                 {
  256.                 d_index[k] = d_index[k-1];
  257.                 k--;
  258.                 }
  259.             d_index[j] = l;
  260.             j = numtolist;
  261.             }
  262.         j++;
  263.         }
  264.     }
  265.  
  266.   if (u_flag == 1)
  267.     {
  268.     j = 0;
  269.     while (j < numtolist)
  270.         {
  271.         if (fido_data.upld > ul_bytes[u_index[j]])
  272.             {
  273.             strcpy(u_name[u_index[numtolist-1]],fido_data.name);
  274.             strcpy(u_call[u_index[numtolist-1]],fido_data.date);
  275.             ul_bytes[u_index[numtolist-1]] = fido_data.upld;
  276.             l = u_index[numtolist-1];
  277.             k = numtolist - 1;
  278.             while (k > j)
  279.                 {
  280.                 u_index[k] = u_index[k-1];
  281.                 k--;
  282.                 }
  283.             u_index[j] = l;
  284.             j = numtolist;
  285.             }
  286.         j++;
  287.         }
  288.     }
  289.  
  290. }
  291.  
  292. if (i == 0)
  293.     helpscrn(9);
  294.  
  295. if (fclose(infile1) != 0)
  296.    helpscrn(5);
  297.  
  298. printf("\nWriting output file");
  299.  
  300. time(&secs_now);
  301.  
  302. if ((c_flag == 1 && d_flag == 1) || (c_flag == 1 && u_flag == 1) || (d_flag == 1 && u_flag == 1))
  303.   strcpy(outline,"Users");
  304. else
  305.   {
  306.   if (c_flag == 1) strcpy(outline,"Callers");
  307.   if (u_flag == 1) strcpy(outline,"Uploaders");
  308.   if (d_flag == 1) strcpy(outline,"Downloaders");
  309.   }
  310.  
  311. /*  Print header to output file  */
  312. if (fprintf(outfile,"\nTop %d %s as of ",numtolist,outline) == EOF) helpscrn(4);
  313. if (fputs(ctime(&secs_now),outfile) == EOF) helpscrn(4);
  314.  
  315.  
  316. if (c_flag == 1)
  317. {
  318. if (fputs("\nCallers:\n",outfile) == EOF) helpscrn(4);
  319. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  320. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  321. if (fputs("##       Name                    Last Called             # Calls\n",outfile) == EOF) helpscrn(4);
  322. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  323. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  324.  
  325. i = 0;
  326.  
  327. while (i < numtolist)
  328.     {
  329.     if (fprintf(outfile,"%2d   ",i+1) == EOF) helpscrn(4);
  330.     strcpy(outline,c_name[c_index[i]]);
  331.     strcat(outline,"                         ");
  332.     outline[25] = '\0';
  333.     strcat(outline,c_call[c_index[i]]);
  334.     strcat(outline,"                         ");
  335.     outline[50] = '\0';
  336.     if (fputs(outline,outfile) == EOF) helpscrn(4);
  337. /*    if (fprintf(outfile,"%s\n",pad_string(add_commas(c_calls[c_index[i]]),10)) == EOF) helpscrn(4); */
  338.     strcpy(outline,pad_string(add_commas(c_calls[c_index[i]]),10));
  339.     if (fprintf(outfile,"%s\n",outline) == EOF) helpscrn(4);
  340.     i++;
  341.     }
  342. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  343. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  344. }
  345.  
  346. if (d_flag == 1)
  347. {
  348. if (fputs("\nDownloads:\n",outfile) == EOF) helpscrn(4);
  349. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  350. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  351. if (fputs("##       Name                    Last Called             D/L (k)\n",outfile) == EOF) helpscrn(4);
  352. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  353. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  354.  
  355. i = 0;
  356.  
  357. while (i < numtolist)
  358.     {
  359.     if (fprintf(outfile,"%2d   ",i+1) == EOF) helpscrn(4);
  360.     strcpy(outline,d_name[d_index[i]]);
  361.     strcat(outline,"                         ");
  362.     outline[25] = '\0';
  363.     strcat(outline,d_call[d_index[i]]);
  364.     strcat(outline,"                         ");
  365.     outline[50] = '\0';
  366.     if (fputs(outline,outfile) == EOF) helpscrn(4);
  367. /*    if (fprintf(outfile,"%s\n",pad_string(add_commas(dl_bytes[d_index[i]]),10)) == EOF) helpscrn(4); */
  368.     strcpy(outline,pad_string(add_commas(dl_bytes[d_index[i]]),10));
  369.     if (fprintf(outfile,"%s\n",outline) == EOF) helpscrn(4);
  370.     i++;
  371.     }
  372. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  373. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  374. }
  375.  
  376. if (u_flag == 1)
  377. {
  378. if (fputs("\nUploads:\n",outfile) == EOF) helpscrn(4);
  379. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  380. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  381. if (fputs("##       Name                    Last Called             U/L (k)\n",outfile) == EOF) helpscrn(4);
  382. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  383. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  384.  
  385. i = 0;
  386.  
  387. while (i < numtolist)
  388.     {
  389.     if (fprintf(outfile,"%2d   ",i+1) == EOF) helpscrn(4);
  390.     strcpy(outline,u_name[u_index[i]]);
  391.     strcat(outline,"                         ");
  392.     outline[25] = '\0';
  393.     strcat(outline,u_call[u_index[i]]);
  394.     strcat(outline,"                         ");
  395.     outline[50] = '\0';
  396.     if (fputs(outline,outfile) == EOF) helpscrn(4);
  397. /*    if (fprintf(outfile,"%s\n",pad_string(add_commas(ul_bytes[u_index[i]]),10)) == EOF) helpscrn(4); */
  398.     strcpy(outline,pad_string(add_commas(ul_bytes[u_index[i]]),10));
  399.     if (fprintf(outfile,"%s\n",outline) == EOF) helpscrn(4);
  400.     i++;
  401.     }
  402. if (fputs("------------------------------",outfile) == EOF) helpscrn(4);
  403. if (fputs("-----------------------------------\n",outfile) == EOF) helpscrn(4);
  404. }
  405.  
  406. /*  Print footer to output file  */
  407. if (fputs("\nProduced by ",outfile) == EOF) helpscrn(4);
  408. if (fputs(title,outfile) == EOF) helpscrn(4);
  409. if (fputs(" v",outfile) == EOF) helpscrn(4);
  410. if (fputs(version,outfile) == EOF) helpscrn(4);
  411. if (fputs("\nCopyright (c) Bob Swift, ",outfile) == EOF) helpscrn(4);
  412. if (fputs(dates,outfile) == EOF) helpscrn(4);
  413. if (fputs(".  All Rights Reserved.\n\n",outfile) == EOF) helpscrn(4);
  414.  
  415. /*  Close the destination file  */
  416. if (fclose(outfile) != 0)
  417.    helpscrn(6);
  418.  
  419. /*  Thank the user and exit gracefully  */
  420. printf("\n\nOperation complete.  Thank-you for using %s.\n\n",title);
  421. exit(0);
  422. }
  423.  
  424. /*******************************************************************/
  425.  
  426. int is_in_dir(char *filename)
  427. /*  Check if input file in current directory  */
  428. {
  429. if (strchr(filename,'\\')!=NULL || strchr(filename,':')!=NULL)
  430.    return(1);
  431.  else
  432.    return(0);
  433. }
  434.  
  435. /*******************************************************************/
  436.  
  437. void add_period(char filename[])
  438. /*  If input file has no extension finish it with a period  */
  439. {
  440. if (strchr(filename,'.')==NULL)
  441.    strcat(filename,".");
  442. }
  443.  
  444. /*******************************************************************/
  445.  
  446. void build_bak_name(char filename[])
  447. /*  Build file name for backup file  */
  448. {
  449. int flag;
  450. for (flag=0;filename[flag]!='.';flag++)
  451.    {
  452.    /*  NULL Line  */
  453.    }
  454. filename[flag] = '\0';
  455. strcat(filename,".BAK");
  456. }
  457.  
  458. /*******************************************************************/
  459.  
  460. int del_backup(char *filename)
  461. /*  Check for existing backup file and delete  */
  462. {
  463. FILE *tempfile;
  464. tempfile=fopen(filename,"r");
  465. fclose(tempfile);
  466. if (tempfile != NULL)
  467.    {
  468.    if (unlink(filename) != 0)
  469.       return(1);
  470.    }
  471. return(0);
  472. }
  473.  
  474. /*******************************************************************/
  475.  
  476. int make_backup(char *srcfile, char *destfile)
  477. /*  Rename source file to destination file  */
  478. {
  479. FILE *tempfile;
  480. tempfile=fopen(srcfile,"r");
  481. fclose(tempfile);
  482. if (tempfile != NULL)
  483.    {
  484.    if (rename(srcfile,destfile) != 0)
  485.       return(1);
  486.    }
  487. return(0);
  488. }
  489.  
  490. /*******************************************************************/
  491.  
  492. int linetest(char *linetst1, char *inline)
  493. /*  This is the routine that actually checks for a match  */
  494. {
  495. int j;
  496. for ( j=0; linetst1[j] == inline[j]; j++ )
  497.     if (linetst1[j+1] == '\0') return(1);
  498. return(0);
  499. }
  500.  
  501. /*******************************************************************/
  502.  
  503. char *add_commas(unsigned int ul)
  504. /*  This routine returns a string of an unsigned integer with commas  */
  505. {
  506. int i;
  507. int j;
  508. char temp[35];
  509. char work[35];
  510. itoa(ul,work,10);
  511. strrev(work);
  512. i=1;
  513. j=1;
  514. temp[0] = work[0];
  515. while(work[i] != '\0')
  516.   {
  517.   if (i%3 == 0)
  518.     {
  519.     temp[j] = ',';
  520.     j++;
  521.     }
  522.   temp[j] = work[i];
  523.   i++;
  524.   j++;
  525.   }
  526. temp[j] = '\0';
  527. strrev(temp);
  528. return(temp);
  529. }
  530.  
  531. /*******************************************************************/
  532.  
  533. char *pad_string(char *str_to_pad, int str_len)
  534. /*  This routine adds leading spaces to a string to a specified length  */
  535. {
  536. char temp[35];
  537. int i;
  538. strcpy(temp,str_to_pad);
  539. strrev(temp);
  540. i = strlen(temp);
  541. while (i < str_len)
  542.     {
  543.     temp[i] = ' ';
  544.     i++;
  545.     }
  546. temp[i] = '\0';
  547. strrev(temp);
  548. return(temp);
  549. }
  550.  
  551. /*******************************************************************/
  552.  
  553. void helpscrn(int ernum)
  554. /*  Here are the error messages and VERY brief instructions  */
  555. {
  556. printf("\n\n");
  557. switch (ernum) {
  558.  
  559. case  1 : printf("Missing/Bad/Extra command line argument\n\n");
  560.           break;
  561.  
  562. case  2 : printf("Unable to open the USERSx.SOC file\n\n");
  563.           break;
  564.  
  565. case  3 : printf("Unable to open the output file\n\n");
  566.           break;
  567.  
  568. case  4 : printf("Unable to write the output file\n\n");
  569.           break;
  570.  
  571. case  5 : printf("Unable to close the USERSx.SOC file\n\n");
  572.           break;
  573.  
  574. case  6 : printf("Unable to close the output file\n\n");
  575.           break;
  576.  
  577. case  7 : printf("Output file not in current directory\n\n");
  578.           break;
  579.  
  580. case  8 : printf("Unable to create output backup file\n\n");
  581.           break;
  582.  
  583. case  9 : printf("No user records to process\n\n");
  584.           break;
  585.  
  586.     }
  587.  
  588. printf("   This program is used to read the Fido 12 CALLER.SYS files and ");
  589. printf("generate a\n   file listing the top users (upload, download, call");
  590. printf("s) for the BBS.  The\n   CALLER.SYS file must be in the current d");
  591. printf("irectory.\n\nThe format for using this program is as follows:\n");
  592. printf("\n    FIDO-TOP <destfile> [options]\n\nWhere <destfile>  is the nam");
  593. printf("e of the output file created\n      [options]   type of analysis t");
  594. printf("o perform (U = Uploads, D = Downloads,\n                          ");
  595. printf("                     C = Number of Calls)\n\nExamples:        FIDO-");
  596. printf("TOP topusers.txt udc (udc is default)\n                 FIDO-TOP up");
  597. printf("loads.top u\n\n");
  598.  
  599. exit(ernum);
  600. }
  601.  
  602. /*******************************************************************/
  603.  
  604.